package com.bee.littlebeetravel.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bee.littlebeetravel.R;
import com.bee.littlebeetravel.utils.AppManager;
import com.bee.littlebeetravel.utils.ToastUtil;

import net.grandcentrix.tray.AppPreferences;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 验证码输入页面
 */
public class VerCodeActivity extends BaseActivity {
    @BindView(R.id.et_one)
    EditText et_one;
    @BindView(R.id.et_two)
    EditText et_two;
    @BindView(R.id.et_three)
    EditText et_three;
    @BindView(R.id.et_four)
    EditText et_four;
    @BindView(R.id.et_five)
    EditText et_five;
    @BindView(R.id.et_six)
    EditText et_six;
    @BindView(R.id.tv_send_ver_code)
    TextView tv_send_ver_code;


    private String testCode = "112233";
    private int arr[] = new int[6];

    // 验证码倒计时
    private CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            long time = millisUntilFinished / 1000;
            tv_send_ver_code.setText(time + "S");
            tv_send_ver_code.setClickable(false);
        }

        @Override
        public void onFinish() {
            tv_send_ver_code.setText("重新获取验证码");
            tv_send_ver_code.setClickable(true);
        }
    };

    @OnClick({R.id.iv_back, R.id.tv_send_ver_code})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_send_ver_code:
                countDownTimer.start();
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ver_code);
        ButterKnife.bind(this);
        AppManager.getInstance().addActivity(this);
        countDownTimer.start();
        initInputSetting();


    }

    /**
     * 输入框焦点切换和监听
     */
    private void initInputSetting() {
        showSoftInputFromWindow(et_one);
        et_one.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String numbe = editable.toString();
                if (!numbe.equals("")) {
                    arr[0] = Integer.parseInt(numbe);
                    showSoftInputFromWindow(et_two);
                }
            }
        });

        et_two.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String numbe = editable.toString();
                if (!numbe.equals("")) {
                    arr[1] = Integer.parseInt(numbe);
                    showSoftInputFromWindow(et_three);
                }
            }
        });

        et_two.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                    showSoftInputFromWindow(et_one);
                }
                return false;
            }
        });

        et_three.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String numbe = editable.toString();
                if (!numbe.equals("")) {
                    arr[2] = Integer.parseInt(numbe);
                    showSoftInputFromWindow(et_four);
                }
            }
        });

        et_three.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                    showSoftInputFromWindow(et_two);
                }
                return false;
            }
        });

        et_four.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String numbe = editable.toString();
                if (!numbe.equals("")) {
                    arr[3] = Integer.parseInt(numbe);
                    showSoftInputFromWindow(et_five);
                }
            }
        });

        et_four.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                    showSoftInputFromWindow(et_three);
                }
                return false;
            }
        });

        et_five.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String numbe = editable.toString();
                if (!numbe.equals("")) {
                    arr[4] = Integer.parseInt(numbe);
                    showSoftInputFromWindow(et_six);
                }
            }
        });

        et_five.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                    showSoftInputFromWindow(et_four);
                }
                return false;
            }
        });

        et_six.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String numbe = editable.toString();
                if (!numbe.equals("")) {
                    arr[5] = Integer.parseInt(numbe);
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < arr.length; i++) {
                        sb.append(arr[i]);
                    }
                    if (sb.toString().equals("112233")) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        AppPreferences appPreferences = new AppPreferences(VerCodeActivity.this);
                        appPreferences.put("isLoginSuccess", "1");
                        startActivity(intent);
                        AppManager.getInstance().finishAllActivity();


                    } else {
                        ToastUtil.showShort(getApplicationContext(), "验证码不正确");
                        Vibrator vibrator = (Vibrator) VerCodeActivity.this.getSystemService(VerCodeActivity.VIBRATOR_SERVICE);
                        vibrator.vibrate(1000);
                        et_one.setText("");
                        et_two.setText("");
                        et_three.setText("");
                        et_four.setText("");
                        et_five.setText("");
                        et_six.setText("");
                        showSoftInputFromWindow(et_one);
                    }
                }
            }
        });

        et_six.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                    showSoftInputFromWindow(et_five);
                }
                return false;
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            // 销毁计时器
            countDownTimer.cancel();
            countDownTimer.onFinish();
            countDownTimer = null;
        }
    }

    /**
     * 获取焦点并弹出软键盘
     *
     * @param editText
     */
    public void showSoftInputFromWindow(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        VerCodeActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

}